package nf.com.crud.dao.impl;

import nf.com.crud.entity.Test2;
import nf.com.crud.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class QueryRunnerWrapper {
    private QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource(),true);

    public int update(String sql,Object... params){
        try {
            return queryRunner.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("数据库增删改操作失败");
        }
    }

    public <T> List<T> query(String sql,Class<T> type,Object... params){
        try {
            return queryRunner.query(sql,new BeanListHandler<T>(type),params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("数据库查询操作失败");
        }
    }
}
